package com.orcamento.entity;

import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;

import com.orcamento.entity.enums.Month;
import com.orcamento.util.Money;

@Entity
@Table(name="Ganho")
public class Gain {
	
	@Id
	@SequenceGenerator(name="gain_id", sequenceName="gain_id")
	@GeneratedValue(generator="gain_id", strategy=GenerationType.AUTO)
	@Column(name="IDGanho")
	private Long id;
	@Embedded
	@AttributeOverride(name="value", column=@Column(name="FoValor", nullable=false))
	private Money value;
	@Column(name="DtDiaRecebimento", nullable=false)
	@Type(
			type="org.jadira.usertype.dateandtime.joda.PersistentDateTime", 
			parameters = { 
					@Parameter(name = "databaseZone", value = "jvm"),
					@Parameter(name = "javaZone", value = "jvm")
			}
		 )
	private DateTime receivedDay;
	
	@ManyToOne(targetEntity=Event.class)
	@JoinColumn(name="IDEvento", referencedColumnName="IDEvento", nullable=false)
	private Event event;
	@ManyToOne(targetEntity=Fund.class)
	@JoinColumn(name="IDFundo", referencedColumnName="IDFundo", nullable=false)
	private Fund fund;
	@ManyToOne(targetEntity=Account.class)
	@JoinColumn(name="IDConta", referencedColumnName="IDPatrimonio", nullable=false)
	private Account account;

	public Boolean isIn(Integer year, Month month) {
		if( month == null || year == null ) {
			return true;
		}
		return receivedDay.getYear() == year && receivedDay.getMonthOfYear() == month.id;
	}
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Money getValue() {
		return value;
	}
	public void setValue(Money value) {
		this.value = value;
	}
	public DateTime getReceivedDay() {
		return receivedDay;
	}
	public void setReceivedDay(DateTime receivedDay) {
		this.receivedDay = receivedDay;
	}
	public Event getEvent() {
		return event;
	}
	public void setEvent(Event event) {
		this.event = event;
	}
	public Fund getFund() {
		return fund;
	}
	public void setFund(Fund fund) {
		this.fund = fund;
	}
	public Account getAccount() {
		return account;
	}
	public void setAccount(Account account) {
		this.account = account;
	}
	
}
